/*************************
 * Variables globales
 */
var __debug = true;

/*************************
 * Redéfinition de fonctions ou de plugin
 */
// Redefinition de Array.indexOf pour ie, safari et opera
if (!Array.indexOf) {
  Array.prototype.indexOf = function (obj, start) {
    for (var i = (start || 0); i < this.length; i++) {
      if (this[i] == obj) {
        return i;
      }
    }
    return -1;
  }
}

// Redefinition du plugin dropdown de bootstrap v2.0.0
$.extend($.fn.dropdown.Constructor.prototype, {
	// Ajout de align dans la fonction toggle
	toggle: function ( e ) {
    var $this = $(this)
      , selector = $this.attr('data-target')
      , align = $this.attr('data-align')
      , $parent
      , isActive
      
    if (!selector) {
      selector = $this.attr('href')
      selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
    }
    
    if (!align)	align = 'left'

    $parent = $selector = $(selector)
    if($parent.length == 0){
    	$parent = $this.parent() 
    	$selector = $this.next('ul')
    }

    isActive = $parent.hasClass('open')

    $('[data-toggle="dropdown"]').parent().removeClass('open')
    if(!isActive) $parent.toggleClass('open')
    
    if (align == 'right') $selector.removeClass('dropdown-menu-left').addClass('dropdown-menu-right');
    else $selector.removeClass('dropdown-menu-right').addClass('dropdown-menu-left');
    
    return false
  }
}); 

/*************************
 * Fonctions communes lancées au chargement de la page
 */
$(document).ready(function(){
	$(".alert .close").click(function(){
		$(this).parents(".alert").hide();
		return false;
	});
	
	$(".modal .close").click(function(){
		$(this).parents(".modal").modal('hide');
		return false;
	});
});

/*************************
 * Fonctions communes
 */
function __dynamicScriptLoad(sUrl, callBack, index) {
	if(typeof sUrl == 'object'){
		$.each(sUrl, function(index, url){
			__dynamicScriptLoad(url, callBack, index);
		});
	} else if(typeof sUrl == 'string'){
		$.getScript(sUrl, function(data, textStatus, jqxhr) {
		   if(__debug) console.log(sUrl+' : ['+jqxhr.status+'] '+textStatus);
		   
		   if(typeof index == 'undefined')
			   callBack(jqxhr.status);
		   callBack(jqxhr.status, index);
		}).fail(function(jqxhr, settings, exception) {
			if(__debug) console.log(exception);
			callBack(500);
		});  
	} 
}

function __dynamicCssLoad(cssUrl) {
	if(typeof cssUrl == 'object'){
		$.each(cssUrl, function(i, url){
			__dynamicCssLoad(url);
		});
	} else if(typeof cssUrl == 'string'){
		console.log(cssUrl);
		$("head").append("<link>");
		css = $("head").children(":last");
		css.attr({
			rel:  "stylesheet",
			type: "text/css",
			href: cssUrl
		});
	}
}

function __effacerErreurs(id){
	$("#"+id+" .error").each(function(){
		$(this).removeClass('error');
	});
	
	$("#"+id+" .txtErreur").each(function(){
		$(this).remove();
	});
	
	$('.nav-tabs img', "#"+id).hide();
}

function __removeValFromString(string,val,separator){
	var array = string.split(separator);
	if(array.indexOf(val) >= 0) array.splice(array.indexOf(val),1);
	return array.join(separator);
}

function __addValFromString(string,val,separator){
	var array = string.split(separator);
	if(array.indexOf("") >= 0) array.splice(array.indexOf(""),1);
	if(array.indexOf(val) < 0) array.push(val);
	return array.join(separator);
}